package mulMan.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import mulMan.domain.AudioFile;
import mulMan.gui.view.AddFileView;
import mulMan.gui.view.AudioFileView;
import mulMan.gui.view.ModifyFileView;
import mulMan.services.AudioFileServices;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AudioFileController{
	
	private AudioFile audioFile;
	private AudioFileView view;
	private AudioFileServices audioFileServices;
	private ApplicationContext context = new ClassPathXmlApplicationContext(
			"application-context.xml");
	
	
	public AudioFile getAudioFile() {
		return audioFile;
	}
	public void setAudioFile(AudioFile audioFile) {
		this.audioFile = audioFile;
	}
	public AudioFileServices getAudioFileServices() {
		return audioFileServices;
	}
	public void setAudioFileServices(AudioFileServices audioFileServices) {
		this.audioFileServices = audioFileServices;
	}

	public AudioFileView getView() {
		return view;
	}
	public void setView(AudioFileView view) {
		this.view = view;
	}
	
	
	public AudioFileController(AudioFile audioFile, AudioFileView view) {
		this.audioFile = audioFile;
		this.view = view;
		audioFileServices = (AudioFileServices)context.getBean("audioFileServices");
		
		if (view instanceof ModifyFileView) {
			((ModifyFileView)view).addModifyAudioFileListener(new ModifyAudioFileListener());
			((ModifyFileView)view).addPlayAudioFileListener(new PlayAudioFileListener());
		}
		
		if (view instanceof AddFileView) {
			((AddFileView)view).addAddAudioFileListener(new AddAudioFileListener());
		}
	}
	
	
	//ACTION LISTENERS
	
	class ModifyAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	audioFile.setTitle(view.getTitleTextField().getText());
	    	audioFile.setAlbum(view.getAlbumTextField().getText());
	    	audioFile.setArtist(view.getArtistTextField().getText());
	    	audioFile.setLength(Integer.valueOf(view.getLengthTextField().getText()));
	    	audioFile.setPath(view.getPathTextField().getText());
	    	try {
	    		System.out.println("ID: " + audioFile.getIdAudioFile());
				audioFileServices.updateAudioFile(audioFile);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    }
   }
	
	class PlayAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	try {
				audioFile.play();
			} catch (UnsupportedAudioFileException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (LineUnavailableException e1) {
				e1.printStackTrace();
			}
	    }
   }
	
	class AddAudioFileListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	    	audioFile.setTitle(view.getTitleTextField().getText());
	    	audioFile.setAlbum(view.getAlbumTextField().getText());
	    	audioFile.setArtist(view.getArtistTextField().getText());
	    	audioFile.setLength(Integer.valueOf(view.getLengthTextField().getText()));
	    	audioFile.setPath(view.getPathTextField().getText());
	    	try {
				audioFileServices.saveAudioFile(audioFile);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    	((AddFileView)view).getButtonAdd().setEnabled(false);
	    }
   }
}
